package cn.akwangl.concurrency.web.model.items.service.impl;

import cn.akwangl.concurrency.web.model.items.mapper.ItemsSpecMapper;
import cn.akwangl.concurrency.web.model.items.pojo.ItemsSpecEntity;
import cn.akwangl.concurrency.web.model.items.service.ItemsSpecService;
import cn.akwangl.concurrency.web.model.order.pojo.ShopCart;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品规格 每一件商品都有不同的规格，不同的规格又有不同的价格和优惠力度，规格表为此设计 服务实现类
 * </p>
 *
 * @author 阿坤同学
 * @since 2022-11-22 14:30:33
 */
@Service
public class ItemsSpecServiceImpl extends ServiceImpl<ItemsSpecMapper, ItemsSpecEntity> implements ItemsSpecService {

    private ItemsSpecMapper itemsSpecMapper;

    @Autowired
    public void setItemsSpecMapper(ItemsSpecMapper itemsSpecMapper) {
        this.itemsSpecMapper = itemsSpecMapper;
    }

    @Override
    public Map<String, ItemsSpecEntity> getItemsSpecEntityMap(List<String> itemSpecIds) {
        Map<String, ItemsSpecEntity> resultMap = new HashMap(itemSpecIds.size());
        List<ItemsSpecEntity> itemsSpecEntities = listByIds(itemSpecIds);
        if (itemsSpecEntities != null && !itemsSpecEntities.isEmpty()) {
            for (ItemsSpecEntity itemsSpecEntity : itemsSpecEntities) {
                String mapKey = itemsSpecEntity.getId();
                ItemsSpecEntity mapValue = itemsSpecEntity;
                resultMap.put(mapKey, mapValue);
            }
        }
        return resultMap;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean decreaseItemSpecStockVersion(List<ShopCart> shopCarts) {
        return itemsSpecMapper.decreaseItemSpecStockVersion(shopCarts) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean decreaseItemSpecStock(List<ShopCart> shopCarts) {
        return itemsSpecMapper.decreaseItemSpecStock(shopCarts) > 0;
    }

    @Override
    public long countStock(List<String> itemsSpecIds) {
        return itemsSpecMapper.countStock(itemsSpecIds);
    }
}
